Vue.component('yl-button', {
    template: `
    <span>
        <el-button :type="type" @click="Export" :loading="loading">
            <slot></slot>
        </el-button>
        <el-table id="ExportData" :data="data" style="display: none; width: 100%;margin-bottom: 20px;">
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column align="center" prop="Log_SerialNumber" label="日志流水号"></el-table-column>
        <el-table-column align="center" prop="Member_Name" label="用户姓名"></el-table-column>
        <el-table-column align="center" prop="Member_SerialNumber" label="用户手机号"></el-table-column>
        <el-table-column align="center" prop="Points_Title" label="账单标题"></el-table-column>
        <el-table-column align="center" prop="Activity_Name" label="活动"></el-table-column>
        <el-table-column align="center" prop="Points" label="类型"  width="70">
            <template v-slot="{row}">
                {{parseInt(row.Points) > 0?'收入':'支出'}}
            </template>
        </el-table-column>
        <el-table-column align="center" prop="Points" label="数字明细"></el-table-column>
        <el-table-column align="center" prop="Points_Type" label="积分类型">
            <template v-slot="{row}">
                {{Type(row.Points_Type)}}
            </template>
        </el-table-column>
        <el-table-column align="center" prop="Remain_Points" label="活动剩余积分"></el-table-column>
        <el-table-column align="center" prop="Operation_Datetime" label="操作时间"></el-table-column>
        </el-table>
    </span>`,
    props: ['loading', 'data', 'type'],
    computed:{
        Type(){
            return function (str){
                switch (str) {
                    case "Cash":
                        return "现金积分";
                    case "Activit":
                        return "活动积分";
                    case "Gift":
                        return "赠送积分";
                    case "Sales":
                        return "消费积分";
                    default:
                        return ""
                }
            }
        }
    },
    methods: {
        Export() {
            this.$emit('click')
        }
    }
})
new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_business_member_points_log",
                PaginationNumber: 1,//页数
                RowAmount: 20,//每页数据条数
                Condition: '',
                ExtensionCondition: '',
                Activity_SerialNumber: '',
                Member_Name: '',
                Points_Title:'',
                Operation_Date: null,
            },
            value1: [],
            Activity: [],
            total: 0,
            tableData: [],	//表格数据
            ExportData:[],//导出
            loading: false,
        }
    },
    mounted() {
        this.getData();
        this.getList(this.form);//前端分页
    },
    computed:{
        Type(){
            return function (str){
                switch (str) {
                    case "Cash":
                        return "现金积分";
                    case "Activit":
                        return "活动积分";
                    case "Gift":
                        return "赠送积分";
                    case "Sales":
                        return "消费积分";
                    default:
                        return ""
                }
            }
        }
    },
    methods: {
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.form)
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getData() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_activity',
                    Condition: "Row_Status <> '1'",
                },
                success:function (res) {
                    that.Activity = res;
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Member_Name + "%,%" + data.Member_Name + "%,%"+ data.Points_Title +"%")))
            let sql = "";
            if (data.Operation_Date) {
                sql = sql + "AND business_member_points_log.Operation_Datetime BETWEEN '" + data.Operation_Date[0] + "' AND '" + data.Operation_Date[1] + "'\r\n"
            }
            if (data.Activity_SerialNumber) {
                sql = sql + "AND business_member_points_log.Activity_SerialNumber = '" + data.Activity_SerialNumber +"'\r\n"
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        downloadTemplate(command) {
            if (command == "Points") {
                window.location.href = './download/内部用户积分变动模板.xlsx';
            }
        },
        importTemplate(command) {
            if (command == "Points") {
                window.location.href = './import.html';
            }
        },
        Export(form){
            const that = this;
            this.loading = true;
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Member_Name + "%,%" + data.Member_Name + "%,%"+ data.Points_Title +"%")))
            let sql = "";
            if (data.Operation_Date) {
                sql = sql + "AND business_member_points_log.Operation_Datetime BETWEEN '" + data.Operation_Date[0] + "' AND '" + data.Operation_Date[1] + "'\r\n"
            }
            if (data.Activity_SerialNumber) {
                sql = sql + "AND business_member_points_log.Activity_SerialNumber = '" + data.Activity_SerialNumber +"'\r\n"
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    if (Result.length > 0) {
                        that.ExportData = Result;
                        that.$nextTick(() => {
                            let wb = XLSX.utils.table_to_book(document.querySelector("#ExportData"));
                            var wbout = XLSX.write(wb, {
                                bookType: "xlsx",
                                bookSST: true,
                                type: "array"
                            });
                            try {
                                saveAs(
                                    new Blob([wbout], {
                                        type: "application/octet-stream"
                                    }),
                                    "积分记录信息表"+ (+new Date()) + ".xlsx"
                                );
                            } catch (e) {
                                if (typeof console !== "undefined") console.log(e, wbout);
                            }
                            that.$message({
                                message: '已成功导出',
                                type: 'success'
                            })
                            return wbout;
                        })
                    } else {
                        that.$message({
                            message: '暂无可导出的数据',
                            type: 'warning'
                        })
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    },
})